using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SIOGDE.AccesoDatos.Entidades;
using SIOGDE.Negocios;
using SIOGDE.Negocios.Servicios;
using Didsoft.Mvc.DataModel;
using Didsoft.Mvc.Controller;

namespace SIOGDE.Web.Controllers
{
    public class RequerimientoConfigController : BaseController<RequerimientoConfig>
    {
	    #region interface servicios RequerimientoConfig

		private IRequerimientoConfigNegociosServicio servicio = BusinessFactory.Current.GetRequerimientoConfigNegocios();

		#endregion
		
		
		#region constructor and overridable

        public RequerimientoConfigController()
        {
            DropdownListStringsProp = "UnidadMilitar.Nombre";
            base.SetDataModelService(servicio);            
        }

		#endregion

		#region	actions

		[HttpGet]
		public override ActionResult Index()
        {
            return RedirectToAction("FilterList");
        }


        public JsonResult GetPorUnidadGuid(string guid)
        {
            try
            {
                List<RequerimientoConfig> items = servicio.GetPorUnidadGuid(guid);
                return Json(new { Result = "OK", Records = items });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = ex.Message });
            }

        }

		#endregion

        #region configurar invocado desde buscador de unidad militar

        [HttpPost]
        public JsonResult JSONPagedListByUM(string UMGuid = "", int jtStartIndex = 0, int jtPageSize = 0, string jtSorting = null)
        {
            try
            {
                List<RequerimientoConfig> searchedItems = servicio.GetPorUnidadGuid(UMGuid);
                var items = searchedItems.Skip(jtStartIndex).Take(jtPageSize).ToList();
                return Json(new { Result = "OK", Records = items, TotalRecordCount = searchedItems.Count() });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = ex.Message });
            }
        }


        public ActionResult Configurar(string GuidUM)
        {
            var unidad = BusinessFactory.Current.GetUnidadMilitarNegocios().GetPorGuid(GuidUM);
            ViewBag.UMGuid = unidad.Guid;

            return View(new RequerimientoConfig() { UnidadMilitar = unidad});
        }

        [HttpPost]
        public JsonResult JSONGuardarConfig(RequerimientoConfig entity)
        {
            try
            {
                //if (!ModelState.IsValid)
                //{
                //    return Json(new { Result = "ERROR", Message = MessageError(ModelState) });
                //}

                var entityCreated = Service.Guardar(entity);

                return Json(new { Result = "OK", Record = entityCreated });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = ex.Message });
            }
        }
        

        #endregion
    }
}

